$(function () {
    var setting = {
        view: {
            dblClickExpand: false
        },
        callback: {
            onRightClick: OnRightClick
        }
    };

    var zTree,rMenu;

    $.ajax({
        url: "/api/permission/findAllSysPermission",
        dataType: "json",
        success: function (data) {
            if(data.code =='401'){
                bootbox.alert(data.message);
            }else{
                $.fn.zTree.init($("#menuTree"), setting, data.data);
                zTree = $.fn.zTree.getZTreeObj("menuTree");
                rMenu = $("#rMenu");
            }
        }
    });

    function OnRightClick (event, treeId, treeNode) {
        if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
            zTree.cancelSelectedNode();
            showRMenu("root", event.clientX, event.clientY,treeNode);
        } else if (treeNode && !treeNode.noR) {
            zTree.selectNode(treeNode);
            showRMenu("node", event.clientX, event.clientY,treeNode);
        }
    }

    function showRMenu (type, x, y,node) {
        $("#rMenu ul").show();
        if (type == "root") {
            $("#m_del").hide();
            $("#m_edit").hide();
            $('#m_add').hide();

            $("#m_add_dir").show();
            $("#m_edit_dir").hide();
            $("#m_del_dir").hide();
        } else {
            if(node.type==1){
                $("#m_add").hide();
                $("#m_del").show();
                $("#m_edit").show();

                $("#m_add_dir").hide();
                $("#m_edit_dir").hide();
                $("#m_del_dir").hide();
            }else{
                $("#m_del").hide();
                $("#m_add").show();
                $("#m_edit").hide();

                $("#m_add_dir").show();
                $("#m_edit_dir").show();
                $("#m_del_dir").show();
            }

        }
        y += document.body.scrollTop;
        x += document.body.scrollLeft;
        rMenu.css({"top": y + "px", "left": x + "px", "visibility": "visible"});

        $("body").bind("mousedown", onBodyMouseDown);
    }

    function hideRMenu () {
        if (rMenu) rMenu.css({"visibility": "hidden"});
        $("body").unbind("mousedown", onBodyMouseDown);
    }

    function onBodyMouseDown (event) {
        if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
            rMenu.css({"visibility": "hidden"});
        }
    }

});

var MenuTree=function () {

    bootbox.setDefaults({
        'locale':'zh_CN'
    });

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

    return{
        add:function () {
            var ztree=$.fn.zTree.getZTreeObj("menuTree");
            var node=ztree.getSelectedNodes()[0];
            if(node){
                $('#addMenu h4').text("添加权限");
                $('#pid').val(node.id);
                $('#label_name').text('权限');
            }
            $('#url_div').show();
            $('#addMenu').modal({
                show:true
            });
        },

        addDir:function () {
            var ztree=$.fn.zTree.getZTreeObj("menuTree");
            var node=ztree.getSelectedNodes()[0];
            if(node == undefined){
                $('#pid').val(0);
            }else{
                $('#pid').val(node.id);
            }
            $('#addMenu h4').text("添加文件夹");
            $('#label_name').text('文件夹');
            $('#type').val(3);
            $('#url_div').hide();
            $('#addMenu').modal({
                show:true
            });
        },

        save:function () {
            var data = $('#menuForm').serializeObject();
            $.ajax({
                url: "/api/permission/save",
                data:JSON.stringify(data),
                type:'POST',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else{
                        if(data.data){
                            $('#addRole').modal("hide");
                            MenuTree.formReset();
                            location.reload();
                        }else{
                            bootbox.alert(data.message)
                        }
                    }
                }
            });
        },
        get:function () {
            var ztree=$.fn.zTree.getZTreeObj("menuTree");
            var node=ztree.getSelectedNodes()[0];
            if(node.type==1){
                $('#addMenu h4').text("编辑权限");
                $('#label_name').text('权限');
                $('#url_div').show();
            }else {
                $('#addMenu h4').text("编辑文件夹");
                $('#label_name').text('文件夹');
                $('#url_div').hide();
            }
            $.ajax({
                url: "/api/permission/getPermission",
                type:"GET",
                data:{id:node.id},
                dataType: "json",
                success: function (data) {
                    if(data.code =='401'){
                        bootbox.alert(data.message);
                    }else{
                        Form.loadData(data.data);
                        $('#addMenu').modal({
                            show:true
                        });
                    }
                }
            });
        },
        delete:function () {
            var ztree=$.fn.zTree.getZTreeObj("menuTree");
            var node=ztree.getSelectedNodes()[0];
            var msg;
            if(node.type==1){
                msg="确定要删除该权限，删除后将不能使用该权限?";
            }else{
                msg='确定要删除该文件夹吗?';
            }
            bootbox.confirm(msg,function(btn){
                if(btn){
                    $.ajax({
                        url: "/api/permission/delete",
                        data:{id:node.id},
                        type:'GET',
                        dataType: "json",
                        success: function (data) {
                            if(data.code =='401'){
                                bootbox.alert(data.message);
                            }else if(data.code =='200' && data.data){
                                location.reload();
                                btn.alert(data.message);
                            }
                        }
                    });
                }
            });
        },
        formReset:function(){
            $('#menuForm')[0].reset();
            $('#id').val('');
            $('#pid').val(0);
            $('#type').val();
            $(':input','#roleForm').removeAttr('checked');
        }

    }
}();


